<?php
namespace Drift\Controller;
use Common\Controller\CommonController;

/**
 * 漂流瓶模块
 */
class DriftController extends CommonController {
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 108;
	}
    
    
	
	/**
	 * 扔瓶
	 */
	public function throwBottleAction(){
		$content = I('request.content');
		$longitude = I('request.longitude');
		$latitude = I('request.latitude');
		$this->_throwBottle($content , $longitude , $latitude);
	}
	
	private function _throwBottle($content , $longitude , $latitude){
		if(empty($content)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$itemKey = \cachekey::$key['BASE_KEY_CONF']['DRIFT_BOTTLE_ID'];
		$itemid = $this->_mycache->incr($itemKey);
		$item = \cachekey::$key['BASE_KEY_CONF']['DRIFT_BOTTLE'] . $itemid;
		
		$driftBottleKey = \cachekey::$key['BASE_KEY_CONF']['DRIFT_BOTTLES'];
		$ret = $this->_mycache->sadd($driftBottleKey , $item);
		if($ret){
			if(!empty($latitude) && !empty($longitude)){
				$geohash = new \Geohash();
				$hash = $geohash->encode($latitude, $longitude);
				$this->_setLocation($latitude , $longitude , $hash);
			}
			
			$data = array();
			$data['itemid'] = (string)$itemid;
			$data['fcustid'] = $this->_custInfo['custid'];
			$data['fnickname'] = $this->_custInfo['nickname'];
			$data['fsex'] = $this->_custInfo['sex'];
			$data['farea'] = $this->_custInfo['area'];
			$data['content'] = $content;
			$data['time'] = (string)time();
			$this->_mycache->set($item , $data);
			
			$this->outPut();
		}else{
			$this->status = 1801;
			$this->msg = L('STATUS_1801');
			$this->outPut();
		}
	}
	
    
	/**
	 * 捞漂流瓶
	 */
	public function getBottleAction(){
		$this->_getBottle();
	}
	
	private function _getBottle(){
		$driftBottleKey = \cachekey::$key['BASE_KEY_CONF']['DRIFT_BOTTLES'];
		$num = 0;
		$item = null;
		$data = array();
		while($num < 5){
			$item = $this->_mycache->spop($driftBottleKey);
			if(empty($item)){
				$num++;
				continue;
			}
			
			$data = $this->_mycache->get($item);
			if(empty($data)){
				$num++;
				continue;
			}
			
			if($data['fcustid'] == $this->_custInfo['custid']){
				$this->_mycache->sadd($driftBottleKey , $item);//捞到自己抛的就放回去
				$num++;
				continue;
			}
			
			break;
		}
		
		if(!empty($item) && !empty($data)){
				$data['tcustid'] = $this->_custInfo['custid'];
				$data['tnickname'] = $this->_custInfo['nickname'];
				$data['tsex'] = $this->_custInfo['sex'];
				$data['tarea'] = $this->_custInfo['area'];
				$this->_mycache->set($item , $data);
				
				$data = $this->_mycache->get($item);
				$this->data = $data;
				$this->outPut();
		}else{
			$this->status = 1803;
			$this->msg = L('STATUS_1803');
			$this->outPut();
		}
	}
	
	
	
	/**
	 * 扔回漂流瓶
	 */
	public function backBottleAction(){
		$itemid = I('request.itemid');
		$item = \cachekey::$key['BASE_KEY_CONF']['DRIFT_BOTTLE'] . $itemid;
		$this->_backBottle($item);
	}
	
	
	private function _backBottle($item){
		if(empty($item)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data = $this->_mycache->get($item);
		if(empty($data) || $data['tcustid'] != $this->_custInfo['custid']){//只能扔回自己捞到的瓶
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$myBottleKey = \cachekey::$key['BASE_KEY_CONF']['CUST_DRIFT_BOTTLES'].$this->_custInfo['custid'];
		$driftBottleKey = \cachekey::$key['BASE_KEY_CONF']['DRIFT_BOTTLES'];
		$ret = $this->_mycache->sadd($driftBottleKey , $item);//放回去
		if($ret){
			$this->outPut();
		}else{
			$this->status = 1805;
			$this->msg = L('STATUS_1805');
			$this->outPut();
		}
	}
	
}
